package com.example.demo.services.impl;

import com.example.demo.dao.HDRepository;
import com.example.demo.entity.HD;
import com.example.demo.entity.HW;
import com.example.demo.entity.YG;
import com.example.demo.services.HDService;
import com.example.demo.services.HWService;
import com.example.demo.services.YGService;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.persistence.EntityManager;
import java.util.List;

@Service
public class HDServiceImpl implements HDService {

    @Autowired
    private HDRepository hdRepository;

    @Autowired
    private YGService ygService;

    @Autowired
    private HWService hwService;

    @Autowired
    private EntityManager em;

    /**
     * @Author: Ding XiJie
     * @Description: 添加员工
     */
    @Override
    public HD save(HD hd) {
        hd.setHdno(RandomUtils.nextInt(100000,1000000)+"");
        HW hw = hwService.getAllHw().get(0);
        //货单名称
        hd.setHdn(hw.getHwn());
        //计算价格
        String hdhwweight = hd.getHdhwweight();
        String weight = hw.getHwweight();
        Integer mob = Integer.parseInt(hdhwweight)/Integer.parseInt(weight);
        hd.setHdprice((mob+1)*Integer.valueOf(hw.getHwprice())+"");
        //获取员工信息
        List<YG> allYg = ygService.getAllYg();
        //随机获取员工发货
        if (!StringUtils.isEmpty(allYg) && allYg.size()>0){
            int i = RandomUtils.nextInt(0,allYg.size()-1);
            YG yg = allYg.get(i);
            hd.setHdygn(yg.getYn());
            hd.setHdygtelephone(yg.getYtelephone());
        }
        if (StringUtils.isEmpty(hd.getHdstatus())){
            hd.setHdstatus("已下单");
        }
        hd = hdRepository.save(hd);
        return hd;
    }

    /**
     * @Author: Ding XiJie
     * @Description: 修改员工
     */
    @Override
    public HD update(HD hd) {
        return hdRepository.save(hd);
    }

    /**
     * @Author: Ding XiJie
     * @Description: 删除员工
     */
    @Override
    public void delete(int id) {
        hdRepository.deleteById(id);
    }

    @Override
    public List<HD> getAllHd() {
        return hdRepository.findAll();
    }

    @Override
    public Integer findCount() {
        Long count = hdRepository.count();
        Integer countInt = count.intValue();
        return countInt;
    }

    /**
     * @Author: Ding XiJie
     * @Description:
     */
    @Override
    public List<HD> getAllHdByKid(Integer kid) {
        return hdRepository.getAllHdByKid(kid);
    }

    /**
     * @Author: Ding XiJie
     * @Description:
     */
    @Override
    public Integer findCountByKid(Integer kid) {
        return hdRepository.findCountByKid(kid);
    }

    /**
     * @Author: Ding XiJie
     * @Description:
     */
    @Override
    public List<HD> getAllHdByHdnoAndKno(Integer kid, Integer hdno) {
        return hdRepository.getAllHdByHdnoAndKno(kid,hdno);
    }

    @Override
    public Integer findCountByKidAndHdno(Integer kid, Integer hdno) {
        return hdRepository.findCountByKidAndHdno(kid,hdno);
    }
}
